<template>
  <div class="addUser">
    <el-form :label-position="labelPosition" status-icon label-width="80px" :model="formLabelAlign" :rules="rules" ref="ruleForm">
      <el-form-item label="名称" prop="name">
        <el-input v-model="formLabelAlign.name"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select v-model="formLabelAlign.sex" placeholder="请选择性别" label-width>
          <el-option label="男" value="男"></el-option>
          <el-option label="女" value="女"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-input v-model.number="formLabelAlign.age"></el-input>
      </el-form-item>
      <el-form-item prop="phone" label="联系方式">
        <el-input v-model="formLabelAlign.phone"></el-input>
      </el-form-item>
      <el-form-item prop="email" label="邮箱">
        <el-input v-model="formLabelAlign.email"></el-input>
      </el-form-item>
      <el-form-item label="账号" prop="account">
        <el-input v-model="formLabelAlign.account"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="pass">
        <el-input type="password" v-model="formLabelAlign.pass" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input type="password" v-model="formLabelAlign.checkPass" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import axios from 'axios'

export default {
  props: ['UserClick'],
  data() {
    var password1 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else if (this.formLabelAlign.pass.length < 6) {
        callback(new Error('密码不能小于6位'))
      } else {
        // if (this.formLabelAlign.checkPass !== '') {
        //   // this.$refs.ruleForm.validateField('checkPass')
        // }
        callback()
      }
    }
    var password2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.formLabelAlign.pass) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入用户名'))
      } else {
        // if (this.formLabelAlign.name !== '') {
        //   // this.$refs.ruleForm.validateField('checkPass')
        // }
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入手机号'))
      } else if (this.formLabelAlign.phone.length < 11) {
        callback(new Error('请输入正确的手机号 太短了哎'))
      } else if (this.formLabelAlign.phone.length > 11) {
        callback(new Error('请输入正确的手机号 太长了哎'))
      } else if (!/^[0-9]*$/.test(this.formLabelAlign.phone)) {
        callback(new Error('请输入正确的手机号! 你怎么能输入字母哎'))
      } else {
        callback()
      }
    }
    var account = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('注册账号'))
      } else {
        if (this.formLabelAlign.account !== '') {
          this.$refs.ruleForm.validateField('checkPass')
        }
        callback()
      }
    }
    return {
      labelPosition: 'right',
      formLabelAlign: {
        name: '',
        sex: '男',
        age: '',
        phone: '',
        email: '',
        pass: '',
        checkPass: '',
        account: ''
      },
      rules: {
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
        ],
        age: [
          { required: true, message: '年龄不能为空' },
          { type: 'number', message: '年龄必须为数字值' }
        ],
        pass: [{ validator: password1, trigger: 'blur' }],
        checkPass: [{ validator: password2, trigger: 'blur' }],
        name: [{ validator: validatePass, trigger: 'blur' }],
        phone: [{ validator: validatePass2, trigger: 'blur' }],
        sex: [],
        account: [{ validator: account, trigger: 'blur' }]
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          axios({
            url: 'http://localhost:3000/user',
            method: 'POST',
            data: {
              name: this.formLabelAlign.name,
              sex: this.formLabelAlign.sex,
              age: this.formLabelAlign.age,
              phone: this.formLabelAlign.phone,
              email: this.formLabelAlign.address,
              account: this.formLabelAlign.account,
              password: this.formLabelAlign.pass,
              isAdmin: '0'
            }
          })
            .then(result => {
              this.UserClick()
              console.log(result)
            })
            .catch(err => {
              console.log(err)
              this.$message.error('注意是否开启服务器！！ ')
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
.addUser {
  text-align: center;
}
.el-select {
  width: 100%;
}
</style>